package com.auxgroup.hr.front.service.hireperson.impl;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.dao.domain.PsCHirePrimaryClass;
import com.auxgroup.hr.front.dao.domain.PsCHireTrainT;
import com.auxgroup.hr.front.dao.repository.PsCHireTrainTRepository;
import com.auxgroup.hr.front.service.hireperson.HireTrainService;

/** 
 * @author lvliuhao
 * @version 创建时间：180523 
 * 入职人员教育经历service
 */
@Service("hireTrainService")
public class HireTrainServiceImpl implements HireTrainService {
	private static final Logger logger = LoggerFactory.getLogger(HireEducatServiceImpl.class);
	
	@Autowired
	private PsCHireTrainTRepository PsCHireTrainTRepository;


	@Override
	public PsCHireTrainT getHireTrain(PsCHirePrimaryClass hirePrimary) {
		return PsCHireTrainTRepository.findOne(hirePrimary);
	}


	@Override
	public PsCHireTrainT save(PsCHireTrainT HireTrain) {
		if(HireTrain.getcSeqno()==null){
			HireTrain.setHrsRowAddOprid(HireTrain.getRsHireId());
			HireTrain.setHrsRowAddDttm(new Timestamp(System.currentTimeMillis()));
		}
		HireTrain.setHrsRowUpdOprid(HireTrain.getRsHireId());
		HireTrain.setHrsRowUpdDttm(new Timestamp(System.currentTimeMillis()));
		Set<String> ignoreFields = new HashSet<String>(Arrays.asList("cSeqno"));
		ObjectFieldUtils.handleNullValue(HireTrain, ignoreFields);
		return PsCHireTrainTRepository.save(HireTrain);
	}


	@Override
	public Boolean delete(PsCHirePrimaryClass hirePrimary) {
		try {
			PsCHireTrainTRepository.delete(hirePrimary);
		} catch (Exception e) {
			logger.error("delete HireTrain error: hirePrimary={}",hirePrimary,e);
			return false;
		}
		return true;
	}
	
	
	@Override
	public List<PsCHireTrainT> getAll(String rsHireId) {
		PsCHireTrainT HireTrain = new PsCHireTrainT();
		HireTrain.setRsHireId(rsHireId);
		return PsCHireTrainTRepository.findAll(Example.of(HireTrain),new Sort(Sort.Direction.DESC,"startDt"));
	}


	
	
	
}
